package com.work.straighttalk;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.facebook.drawee.view.SimpleDraweeView;
import com.work.straighttalk.app.StraightTalk;
import com.work.straighttalk.ui.MainActivity;

import org.w3c.dom.Text;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobUser;

/**
 * Created by dllo on 16/3/15.
 */
public class FriendsCircleAdapter extends RecyclerView.Adapter {
    private List<FriendsTexts> data;
    private int TYPE0 = 0;
    private int TYPE1 = 1;
    private Context context;


    public void addData(List<FriendsTexts> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    public FriendsCircleAdapter() {
        this.context = StraightTalk.getContext();
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE0;
        } else {
            return TYPE1;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == TYPE0) {
            view = LayoutInflater.from(context).inflate(R.layout.friends_head, null);
            return new HeadViewHolder(view);
        } else {
            view = LayoutInflater.from(context).inflate(R.layout.friends_item, null);
            return new ItemViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        BmobUser currentUser = BmobUser.getCurrentUser(context);
        if (holder instanceof HeadViewHolder) {
            ((HeadViewHolder) holder).name.setText(currentUser.getUsername());
        } else if (holder instanceof ItemViewHolder) {
//            ((ItemViewHolder) holder).name.setText(data.get(position - 1).getName());
//            ((ItemViewHolder) holder).text.setText(data.get(position - 1).getText());
//            ((ItemViewHolder) holder).love.setText(data.get(position - 1).getLove() + "");

//            Uri uri = Uri.parse(data.get(position - 1).getContentfigureurl().getFileUrl(context));
//            ((ItemViewHolder) holder).image.setImageURI(uri);

        }
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size() + 1;
    }

    class HeadViewHolder extends RecyclerView.ViewHolder {
        TextView name;

        public HeadViewHolder(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.head_name);
        }
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        private ImageView photo;
        private SimpleDraweeView image;
        private TextView name, text, love;

        public ItemViewHolder(View itemView) {
            super(itemView);
            love = (TextView) itemView.findViewById(R.id.item_love);
            photo = (ImageView) itemView.findViewById(R.id.item_photo);
            name = (TextView) itemView.findViewById(R.id.item_name);
            text = (TextView) itemView.findViewById(R.id.item_text);
            image = (SimpleDraweeView) itemView.findViewById(R.id.item_image);
        }
    }

    /**
     * 获取屏幕宽高
     *
     * @param activity
     * @return
     */
    public static int[] getScreenSize() {
        int[] screens;
        // if (Constants.screenWidth > 0) {
        // return screens;
        // }
        DisplayMetrics dm = new DisplayMetrics();
        dm = StraightTalk.getContext().getResources().getDisplayMetrics();
        screens = new int[]{dm.widthPixels, dm.heightPixels};
        return screens;
    }


    public static float[] getBitmapConfiguration(Bitmap bitmap, ImageView imageView, float screenRadio) {
        int screenWidth = getScreenSize()[0];
        float rawWidth = 0;
        float rawHeight = 0;
        float width = 0;
        float height = 0;
        if (bitmap == null) {
            // rawWidth = sourceWidth;
            // rawHeight = sourceHeigth;
            width = (float) (screenWidth / screenRadio);
            height = (float) width;
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        } else {
            rawWidth = bitmap.getWidth();
            rawHeight = bitmap.getHeight();
            if (rawHeight > 10 * rawWidth) {
                imageView.setScaleType(ImageView.ScaleType.CENTER);
            } else {
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            }
            float radio = rawHeight / rawWidth;

            width = (float) (screenWidth / screenRadio);
            height = (float) (radio * width);
        }
        return new float[]{width, height};
    }
}
